﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using MirrorDriver;
using MySql.Data.MySqlClient;
using Providers.LiveControl.Server.ScreenHelper;
using  RectDrawing =System.Drawing.Rectangle ;
namespace ScreenTest
{
    class Program
    {

        /// <summary>
        ///     键盘事件
        /// </summary>
        /// <param name="bVk">虚拟键值</param>
        /// <param name="bScan">定义该键的硬件扫描码，一般为0</param>
        /// <param name="dwFlags">这里是整数类型  0 为按下，2为释放</param>
        /// <param name="dwExtraInfo">这里是整数类型 一般情况下设成为 0</param>
        [DllImport("user32.dll", EntryPoint = "keybd_event")]
        public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        static void Main(string[] args)
        {
            TestScreenDevice();
            //TestThreadPool();
            //TestGridNode();
            //Test2Dim();
            //TestSpiltRect(new RectDrawing(10, 10, 20, 20), 3);
            //TestCursorApi();
            //TesRectangle();
            //TestEvent();
            //TestKeyEvent();
            //TestQueue();
            //TestList();
            //TestConnect();
            //TestScrean();
            //TestCwScreen(1);
            //TestMirror();
            Console.ReadLine();
        }

        private static void TestThreadPool()
        {
            System.Threading.WaitCallback waitCallback = new WaitCallback(MyThreadWork);

            int i = 0;
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            Console.WriteLine("1232456");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            ThreadPool.QueueUserWorkItem(waitCallback, $"第{i++}个线程");
            Console.ReadLine();
        }
        public static void MyThreadWork(object state)
        {
            Console.WriteLine("线程现在开始启动…… {0}", (string)state);
            Thread.Sleep(100000);
            Console.WriteLine("运行结束…… {0}", (string)state);
        }

        private static void TestGridNode()
        {
            GridNodeManager tGridNodeManager = new GridNodeManager();
            tGridNodeManager.UpdateNodeTreeCount();
            tGridNodeManager.Test();
            do
            {
                Console.ReadLine();
                Console.WriteLine("---------------------------------");
                tGridNodeManager.Test();
            } while (true);
           

        }

        private static void Test2Dim()
        {
            int[,] array = new int[3, 2];
            array[0, 0] = 1;
            array[1, 0] = 2;
            array[2, 0] = 3;
            array[0, 1] = 4;
            array[1, 1] = 5;
            array[2, 1] = 6;

            Console.WriteLine("数组的长度为{0}", array.Length);

            foreach (var i in array)
            {
                Console.WriteLine(i);
            }

            Console.WriteLine(array.GetLength(0));
            Console.WriteLine(array.GetLength(1));

            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    Console.WriteLine(array[i, j]);
                }
            }
        }

        private static void SpiltLine(int[] lines, int dx, int totalLength ,int _dimesion)
        {
            for (int i = 0; i < _dimesion - 1; i++)
            {
                lines[i] = dx;
            }
            lines[_dimesion - 1] = totalLength - dx * (_dimesion - 1);
        }
        private static void TestSpiltRect(RectDrawing tRectangle ,int _dimesion)
        {
            RectDrawing[,] _rectGridsChild = new RectDrawing[_dimesion,_dimesion];
            RectDrawing _rect = tRectangle;
            if (_dimesion == 0)
                return;
            int width = _rect.Width;
            int height = _rect.Height;

            int dx = _rect.Width / _dimesion;
            int dy = _rect.Height / _dimesion;

            int[] widths = new int[_dimesion];
            int[] heights = new int[_dimesion];

            SpiltLine(widths, dx, width, _dimesion);
            SpiltLine(heights, dy, height,_dimesion);

            int currentX = _rect.X;
            for (int i = 0; i < widths.Length; i++)
            {
                int currentY = _rect.Y;
                for (int j = 0; j < heights.Length; j++)
                {
                    Console.WriteLine($"x={currentX},y={currentY},width={widths[i]},height={heights[j]} ");
                    //_rectGridsChild[i, j].SetRect(
                    //    new RectDrawing(currentX, currentY, widths[i], heights[j])
                    //   );
                    currentY += heights[j];
                }
                currentX += widths[i];
            }

        
    }
        private static void TestCursorApi()
        {

            int MOUSEEVENTF_LEFTDOWN = 0x2;
            int MOUSEEVENTF_LEFTUP = 0x4;
            int MOUSEEVENTF_MIDDLEDOWN = 0x20;
            int MOUSEEVENTF_MIDDLEUP = 0x40;
            int MOUSEEVENTF_MOVE = 0x1;
            int MOUSEEVENTF_ABSOLUTE = 0x8000;
            int MOUSEEVENTF_RIGHTDOWN = 0x8;
            int MOUSEEVENTF_RIGHTUP = 0x10;
            int MOUSEEVENTF_WHEEL = 2048;
            while (true)
            {
                POINT pt;
                GetCursorPos(out pt);
                Console.WriteLine($"pt.X:{pt.X},pt.Y:{pt.Y}");

                Cursor.Position = new System.Drawing.Point(111, 147);
                Console.ReadLine();
                mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            }
        }
        [DllImport("user32.dll")]
        public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
        [DllImport("user32.dll")]
        public static extern bool GetCursorPos(out POINT lpPoint);

        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;
            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }
        class  TestRect
        {
            public RectDrawing TRectangle;
        }
        private static void TesRectangle()
        {
            TestRect tr =new TestRect();
            tr.TRectangle = new RectDrawing(100, 100, 300, 300);
            Console.WriteLine(tr.TRectangle);

            Offset(tr);
            Console.WriteLine(tr.TRectangle);

        }

        private static void Offset(TestRect tr)
        {
            tr.TRectangle.Offset(-100, -100);
        }

        private static void TestCwScreen(int i)
        {
            var index = i;

            index = Screen.AllScreens.Length - 1 < index ? 0 : index;
            var bounds = Screen.AllScreens[index].Bounds;
            Console.WriteLine(bounds);
        }

        private static void TestEvent()
        {
            EventTest t = new EventTest();
            t.Test();
        }

        private static void TestConnect()
        {
            Console.WriteLine("start TestConnect");

            string Conn = "Database='RemoteDB';Data Source='120.26.247.240';User Id='root';Password='123456';charset='utf8';pooling=true";
            string command = "select * from machinetb";
            string FieldPasswordHash = "password";
            string FieldIdentity = "identity";
            var dataRow = MySqlHelper.ExecuteDataRow(Conn, command, null);
            string passwordHash = (string)dataRow[FieldPasswordHash];
            string identity = (string)dataRow[FieldIdentity];
            Console.WriteLine(passwordHash);
            Console.WriteLine(identity);


            string str = passwordHash + identity;
        }
        private static void TestList()
        {
            List<int> intList = new List<int>() {1, 1, 1, 2, 2, 4, 6};
            Console.WriteLine(intList.Count);

            FirstFunc(intList);
            Console.WriteLine("TestList");

            Console.WriteLine(intList.Count);

        }

        private static void FirstFunc(List<int> intList)
        {
            intList.Remove(1);
            intList = NewList(intList);
            Console.WriteLine("FirstFunc");

            Console.WriteLine(intList.Count);
        }

        private static List<int> NewList(List<int> intList)
        {
            intList = new List<int>()
            {
                intList[0],intList[2],intList[4],intList[2],
            };
            return intList;
        }

        private static void TestKeyEvent()
        {
            do
            {
             keybd_event(Keys.S, 0, 0, 0);

            } while (true);
        }

        static  Queue<int> queueTest = new Queue<int>(); 
        private static void TestQueue()
        {
            queueTest.Enqueue(1);
            Thread thread = new Thread(AddLoop);
            thread.Start();
            Thread threadDequque = new Thread(DequeueLoop);
            threadDequque.Start();
            Thread threadDequque2 = new Thread(DequeueLoop);
            threadDequque2.Start();
        }

        private static void DequeueLoop()
        {
            Console.WriteLine(queueTest.Dequeue());
        }

        private static void AddLoop()
        {
            int i = 0;
            do
            {
                queueTest.Enqueue(i++);
            } while (true);
        }


        private static void TestMirror()
        {
           var mirrorDriver = new DesktopMirror();
            mirrorDriver.DesktopChange += MirrorDriver_DesktopChange;
            if (mirrorDriver.State != DesktopMirror.MirrorState.Running)
            {
                // Most likely first time
                // Start the mirror driver
                mirrorDriver.Load();

                mirrorDriver.Connect();
                mirrorDriver.Start();
            }
        }

        private static void MirrorDriver_DesktopChange(object sender, DesktopMirror.DesktopChangeEventArgs e)
        {
            if (Screen.AllScreens.Length <2)
            {
                return;
            }
            var tRectangle = e.Region;
            if (Screen.AllScreens[1].Bounds.IntersectsWith(tRectangle))
            {
                Console.WriteLine(e.Region.ToString());

            }
        }

        private static void TestScreenDevice()
        {
     
            var screens = Screen.AllScreens;
            foreach (var screen in screens)
            {
                Console.WriteLine(screen.DeviceName);
            }
        }
        private static void TestScrean()
        {
            RectDrawing tRectangle = Screen.AllScreens.Aggregate(Screen.PrimaryScreen.Bounds, (current, screen) => RectDrawing.Union(current, screen.Bounds));
            Console.WriteLine(tRectangle);
            var screens = Screen.AllScreens;
            foreach (var screen in screens)
            {

                Console.WriteLine(screen.Bounds.ToString());
                
            }
        }
    }
}
